
import java.util.Vector;
import java.io.*;
/**
 * Created by John on 2016/6/7.
 */
 class Tank
{
    int x;//表示坦克的坐标
    int y;
    boolean isLive=true;
    //0上1下2左3右
    int direct=0;
    int speed=1;
    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }


    public int getDirect() {
        return direct;
    }

    public void setDirect(int direct) {
        this.direct = direct;
    }


    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }

    public void setX(int x) {
        this.x = x;
    }


    public Tank(int x,int y)
    {
        this.x=x;
        this.y=y;
    }

}
class Bomb
{
    int x,y;
    int life=9;
    boolean isLive=true;
    public Bomb(int x,int y)
    {
        this.x=x;
        this.y=y;
    }
    public void lifeDown()
    {
        if (life>0)
            life--;
        else
            this.isLive=false;
    }
}
class Node
{
    int x;
    int y;
    int direct;
    public Node(int x,int y,int direct)
    {
        this.x=x;
        this.y=y;
        this.direct=direct;
    }
}
//记录类，同时保存一些设置
class Recorder
{
    public static int getMyLife() {
        return myLife;
    }

    public static void setMyLife(int myLife) {
        Recorder.myLife = myLife;
    }

    public static int getEnNum() {
        return enNum;
    }

    public static void setEnNum(int enNum) {
        Recorder.enNum = enNum;
    }

    public static int getAllEnNum() {
        return allEnNum;
    }

    public static void setAllEnNum(int allEnNum) {
        Recorder.allEnNum = allEnNum;
    }
    private static FileWriter fw=null;
    private static BufferedWriter bw=null;
    private static FileReader fr=null;
    private static BufferedReader br=null;
    static Vector<Node> nodes=new Vector<Node>();
    //完成读取任务
    public Vector<Node> getNodesAndEnNums()
    {
        try {
            fr=new FileReader("d:\\myRecording.txt");
            br=new BufferedReader(fr);


            String n="";
            n=br.readLine();
            allEnNum=Integer.parseInt(n);
            while ((n=br.readLine())!=null)
            {
                String []xyz=n.split(" ");

                Node node=new Node(Integer.parseInt(xyz[0]),Integer.parseInt(xyz[1]),Integer.parseInt(xyz[2]));
                nodes.add(node);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return nodes;
    }
    public  Vector<EnemyTank> getEts() {
        return ets;
    }

    public void setEts(Vector<EnemyTank> ets) {
        this.ets = ets;
    }

    private Vector<EnemyTank> ets=new Vector<EnemyTank>();
    //保存击毁敌人坦克的坐标方向
    public void keepRecAndEnemyTank()
    {
        try {
            fw=new FileWriter("d:\\myRecording.txt");
            bw=new BufferedWriter(fw);
            bw.write(allEnNum+"\r\n");
            //保存当前活着的敌人坦克
            for (int i=0;i<ets.size();i++)
            {
                System.out.println(ets.size());
                //取出第一个坦克
                EnemyTank et=ets.get(i);
                if (et.isLive)
                {
                    //活着就保存
                    String recode=et.x+" "+et.y+" "+et.direct;
                    //写入文件
                    bw.write(recode+"\r\n");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //从文件中读取记录
    public static void getRecording()
    {
        try {
            fr=new FileReader("d:\\myRecording.txt");
            br=new BufferedReader(fr);
            String n=br.readLine();
            allEnNum=Integer.parseInt(n);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //把玩家击毁敌人坦克数量保存到文件中
    public static void keepRecording()
    {
        try {
            fw=new FileWriter("d:\\myRecording.txt");
            bw=new BufferedWriter(fw);
            bw.write(allEnNum+"\r\n");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //记录消灭多少坦克
    private static  int allEnNum=0;
    //记录每关有多少敌人
    private static int enNum=4;
    //设置我有多少可以用的人
    private static  int myLife=3;
    public static void reduceEnNun()
    {
        enNum--;
    }
    public static  void addEnNumRec()
    {
        allEnNum++;
    }
}
//子弹类
class Shot implements Runnable
{
    int x;
    int y;
    int direct;
    int speed=1;
    boolean isLive=true;
    public Shot(int x,int y,int direct)
    {
        this.x=x;
        this.y=y;
        this.direct=direct;
    }
    public void run()
    {
        while (true)
        {
            try
            {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            switch (direct)
            {
                case 0:
                    y-=speed;
                    break;
                case 1:
                    y+=speed;
                    break;
                case 2:
                    x-=speed;
                    break;
                case 3:
                    x+=speed;
                    break;
            }


            //子弹未亡
            //判断该子弹是否碰到边缘
            if (x<0||x>400||y<0||y>300)
            {
                this.isLive=false;
                break;
            }
        }
    }
}
//我的坦克类
 class Hero extends Tank
{
    //子弹
    Vector<Shot> ss=new Vector<>();

    Shot s=null;
    public Hero(int x,int y)
    {
        super(x,y);
    }
    //坦克向上移动
    public void shotEnemy()
    {
        switch (this.direct)
        {
            case 0:
                s=new Shot(x+10,y,0);
                ss.add(s);
                break;
            case 1:
                s=new Shot(x+10,y+30,1);
                ss.add(s);
                break;
            case 2:
                s=new Shot(x,y+10,2);
                ss.add(s);
                break;
            case 3:
                s=new Shot(x+30,y+10,3);
                ss.add(s);
                break;
        }
        Thread t=new Thread(s);
        t.start();
    }
    public void moveRight()
    {
        x+=speed;
    }
    public void moveUp()
    {
        y-=speed;
    }
    public void moveDown()
    {
        y+=speed;
    }
    public void moveLeft()
    {
        x-=speed;
    }

}
//敌人坦克
class EnemyTank extends Tank implements Runnable
        {
            //定义一个向量 访问mypanel中所有敌人坦克
            Vector<EnemyTank> ets=new Vector<EnemyTank>();
            Vector<Shot> ss=new Vector<Shot>();
            int times=0;
            public EnemyTank(int x,int y)
            {
                super(x,y);
            }
            //得到mypanel 的敌人坦克数量
            public void setEts(Vector<EnemyTank> vv)
            {
                this.ets=vv;
            }
            //判断是否碰到了别的别人坦克
            public boolean isTouchOtherEnemy()
            {
                boolean b=false;
                switch (this.direct)
                {
                    case 0:
                        //我的坦克向上
                        //取出所有的坦克
                        for (int i=0;i<ets.size();i++)
                        {
                            //取出第一个坦克
                            EnemyTank et=ets.get(i);
                            //如果不是自己
                            if (et!=this)
                            {
                                //如果敌人的方向向上或向下
                                if (et.direct==0||et.direct==1)
                                {
                                    if (this.x>=et.x&&this.x<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)
                                    {
                                        return true;
                                    }
                                    if (this.x+20>et.x&&this.x+20<et.x+20&&this.y>=et.y&&this.y<=et.y+30)
                                    {
                                        return true;
                                    }
                                }
                                //如果敌人坦克向左或向右
                                if (et.direct==2||et.direct==3)
                                {
                                    if (this.x>=et.x&&this.x<=et.x+30&&this.y>=et.y&&this.y<=et.y+20)
                                    {
                                        return true;
                                    }
                                    if (this.x+20>et.x&&this.x+20<et.x+30&&this.y>=et.y&&this.y<=et.y+20)
                                    {
                                        return true;
                                    }
                                }
                            }
                        }
                        break;
                    case 1:
                        //我的坦克向下
                        for (int i=0;i<ets.size();i++)
                        {
                            //取出第一个坦克
                            EnemyTank et=ets.get(i);
                            //如果不是自己
                            if (et!=this)
                            {
                                //如果敌人的方向向上或向下
                                if (et.direct==0||et.direct==1)
                                {
                                    if (this.x>=et.x&&this.x<=et.x+20&&this.y+30>=et.y&&this.y<=et.y+30)
                                    {
                                        return true;
                                    }
                                    if (this.x+20>et.x&&this.x+20<et.x+20&&this.y+30>=et.y&&this.y<=et.y+30)
                                    {
                                        return true;
                                    }
                                }
                                //如果敌人坦克向左或向右
                                if (et.direct==2||et.direct==3)
                                {
                                    if (this.x>=et.x&&this.x<=et.x+30&&this.y+30>=et.y&&this.y+30<=et.y+20)
                                    {
                                        return true;
                                    }
                                    if (this.x+20>et.x&&this.x+20<et.x+30&&this.y+30>=et.y&&this.y+30<=et.y+20)
                                    {
                                        return true;
                                    }
                                }
                            }
                        }
                        break;
                    case 2:
                        //我的坦克向左
                        for (int i=0;i<ets.size();i++)
                        {
                            //取出第一个坦克
                            EnemyTank et=ets.get(i);
                            //如果不是自己
                            if (et!=this)
                            {
                                //如果敌人的方向向上或向下
                                if (et.direct==0||et.direct==1)
                                {
                                    if (this.x>=et.x&&this.x<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)
                                    {
                                        return true;
                                    }
                                    if (this.x>et.x&&this.x<et.x+20&&this.y+20>=et.y&&this.y+20<=et.y+30)
                                    {
                                        return true;
                                    }
                                }
                                //如果敌人坦克向左或向右
                                if (et.direct==2||et.direct==3)
                                {
                                    if (this.x>=et.x&&this.x<=et.x+30&&this.y>=et.y&&this.y<=et.y+20)
                                    {
                                        return true;
                                    }
                                    if (this.x>et.x&&this.x<et.x+30&&this.y+20>=et.y&&this.y+20<=et.y+20)
                                    {
                                        return true;
                                    }
                                }
                            }
                        }
                        break;
                    case 3:
                        //坦克向右
                        for (int i=0;i<ets.size();i++)
                        {
                            //取出第一个坦克
                            EnemyTank et=ets.get(i);
                            //如果不是自己
                            if (et!=this)
                            {
                                //如果敌人的方向向上或向下
                                if (et.direct==0||et.direct==1)
                                {
                                    if (this.x+30>=et.x&&this.x+30<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)
                                    {
                                        return true;
                                    }
                                    if (this.x+30>=et.x&&this.x+30<et.x+20&&this.y+20>=et.y&&this.y+20<=et.y+30)
                                    {
                                        return true;
                                    }
                                }
                                //如果敌人坦克向左或向右
                                if (et.direct==2||et.direct==3)
                                {
                                    if (this.x+30>=et.x&&this.x<=et.x+30&&this.y>=et.y&&this.y<=et.y+20)
                                    {
                                        return true;
                                    }
                                    if (this.x+30>et.x&&this.x+30<et.x+30&&this.y+20>=et.y&&this.y+20<=et.y+20)
                                    {
                                        return true;
                                    }
                                }
                            }
                        }
                        break;
                }
                return b;

            }


            @Override
            public void run() {
                while (true)
                {


                switch (this.direct)
                {
                    case 0:
                        for (int i=0;i<3;i++) {
                            if (y>0&&!isTouchOtherEnemy())
                            y -= speed;
                            try{
                                Thread.sleep(50);
                            }catch (Exception e){e.printStackTrace();}
                        }
                        break;
                    case 1:
                        for (int i=0;i<3;i++) {
                            if (y<300&&!isTouchOtherEnemy())
                            y += speed;
                            try{
                                Thread.sleep(50);
                            }catch (Exception e){e.printStackTrace();}
                        }
                        break;
                    case 2:
                        for (int i=0;i<3;i++) {
                            if (x>0&&!isTouchOtherEnemy())
                            x-= speed;
                            try{
                                Thread.sleep(50);
                            }catch (Exception e){e.printStackTrace();}
                        }
                        break;
                    case 3:
                        for (int i=0;i<3;i++) {
                            if (x<400&&!isTouchOtherEnemy())
                            x+= speed;
                            try{
                                Thread.sleep(300);
                            }catch (Exception e){e.printStackTrace();}
                        }
                        break;
                }
                    //让坦克产生随机方向
                    this.direct=(int)(Math.random()*4);
                    //判断敌人坦克是否死亡
                    if (this.isLive==false)
                    {
                        break;
                    }
                    this.times++;
                    if (times%2==0)
                    {
                        if (isLive)
                        {
                            if (ss.size()<5)
                            {
                                //判断是否需要给坦克加入子弹

                                            Shot s=null;
                                            switch (direct)
                                            {
                                                case 0:
                                                    s=new Shot(x+10,y,0);
                                                    ss.add(s);
                                                    break;
                                                case 1:
                                                    s=new Shot(x+10,y+30,1);
                                                    ss.add(s);
                                                    break;
                                                case 2:
                                                    s=new Shot(x,y+10,2);
                                                    ss.add(s);
                                                    break;
                                                case 3:
                                                    s=new Shot(x+30,y+10,3);
                                                    ss.add(s);
                                                    break;
                                            }
                                            Thread t=new Thread(s);
                                            t.start();


                            }


                        }

                    }
                }
            }
        }
